# 这是一个示例 Python 脚本。

# 按 Shift+F10 执行或将其替换为您的代码。
# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。


# def print_hi(name):
#     # 在下面的代码行中使用断点来调试脚本。
#     print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。
import pandas as pd
import numpy as np
from collections import Counter
import json


def diff_tag_read():
    # 读取
    data = pd.read_csv('problem_list.csv')
    alg_diff_tag = data['diff_tag'].reset_index()
    alg_diff_tag.dropna(inplace=True)

    # 整理格式
    alg_diff_tag['diff_tag'] = alg_diff_tag['diff_tag'].apply(lambda row: row.split('&'))

    # 整理难度
    counts = Counter([actor for actors in alg_diff_tag['diff_tag'] for actor in actors])
    diff_tag_res = pd.DataFrame([[x, counts[x]] for x in counts], columns=['tag', 'freq']).sort_values(by='freq',
                                                                                                       ascending=False)
    return diff_tag_res


def Question3():
    data = pd.read_csv('problem_list.csv')
    diff_tag_res = diff_tag_read()

    ac_avgrate = []
    mem_avg = []
    time_avg = []
    len_avg = []

    for diff_tag in diff_tag_res['tag']:

        # 平均正确率
        freq = diff_tag_res.loc[diff_tag_res['tag'] == diff_tag]['freq'].iloc[0]
        # print(freq)
        ac_list = data.loc[data['diff_tag'] == diff_tag][['pid', 'diff_tag', 'ac_rate']]
        ac_list['ac_rate'] = ac_list['ac_rate'].apply(lambda row: float(row.strip('%')))
        ac_avgrate.append(ac_list['ac_rate'].sum() / freq)

        # 找每道题的各种信息
        memory = []
        runtime = []
        code_len = []
        for pid in ac_list['pid']:
            question_file = pd.read_csv('./luogu_data/record/' + pid + ' records.csv')
            if len(question_file) == 0:
                continue
            runtime.append(question_file['runtime/ms'].sum() / len(question_file))
            memory.append(question_file['memory/KB'].sum() / len(question_file))
            code_len.append(question_file['code_len/B'].sum() / len(question_file))

        mem = np.mean(memory)
        time = np.mean(runtime)
        length = np.mean(code_len)

        mem_avg.append(mem)
        time_avg.append(time)
        len_avg.append(length)

    diff_tag_res['ac_rate'] = ac_avgrate
    diff_tag_res['ac_rate'].apply(lambda row: format(row, '.2%'))
    diff_tag_res['mem_avg'] = mem_avg
    diff_tag_res['time_avg'] = time_avg
    diff_tag_res['len_avg'] = len_avg
    print(diff_tag_res)
    diff_tag_res.to_csv('diff_statistics.csv')


def Question4():
    # 整理算法标签
    alg_tags_df = pd.read_csv('problem_list.csv')[['alg_tags', 'diff_tag']]
    alg_tags_df.dropna(inplace=True)
    alg_tags_df['alg_tags'] = alg_tags_df['alg_tags'].apply(lambda row: row.split('&'))
    # print(alg_tags_df)

    # print([actor for actors in alg_tags_df['alg_tags'] for actor in actors])
    counts = Counter([actor for actors in alg_tags_df['alg_tags'] for actor in actors])
    alg_tags = pd.DataFrame([[x, counts[x]] for x in counts], columns=['tag', 'freq']).sort_values(by='freq',
                                                                                                   ascending=False)
    # 所有算法标签
    alg_tags_res = alg_tags['tag']
    # print(alg_tags_res)

    # 所有难度标签
    diff_tag_res = diff_tag_read()['tag']
    # print(diff_tag_res)

    res_list = pd.DataFrame(data=None, columns=['diff_tag', 'alg_tag', 'freq'])

    json_dic = {}
    val_dic = {}
    # 计算不同难度下不同算法的个数
    for tag in diff_tag_res:

        json_list = []
        # 该难度下的所有题目
        question_list = alg_tags_df.loc[alg_tags_df['diff_tag'] == tag]
        # print(question_list)

        # 统计所有标签出现情况
        counts = Counter([actor for actors in question_list['alg_tags'] for actor in actors])
        # print(counts)
        res_table = pd.DataFrame([[tag, x, counts[x]] for x in counts],
                                 columns=['diff_tag', 'alg_tag', 'freq']).sort_values(by='freq',
                                                                                      ascending=False)

        res_table = res_table.head(30)
        res_list = pd.concat([res_table, res_list], ignore_index=True)
        df = np.array(res_table.loc[:, ['alg_tag', 'freq']])
        for item in df:
            json_list.append({'name': item[0], 'value': item[1]})
        val_dic[tag] = json_list
        df = df.T
        json_dic[tag] = df.tolist()

    # print(res_list)
    res_list.to_csv('alg_diff.csv')

    j_str = json.dumps(json_dic, ensure_ascii=False)
    v_str = json.dumps(val_dic, ensure_ascii=False)
    # print(v_str)
    with open('test1.json', 'w', encoding='UTF-8') as f2:
        # ensure_ascii=False才能输入中文，否则是Unicode字符
        # indent=2 JSON数据的缩进，美观
        json.dump(j_str, f2, ensure_ascii=False, indent=2)

    with open('test2.json', 'w', encoding='UTF-8') as f3:
        # ensure_ascii=False才能输入中文，否则是Unicode字符
        # indent=2 JSON数据的缩进，美观
        json.dump(v_str, f3, ensure_ascii=False, indent=2)


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    # Question3()
    Question4()
